﻿#include "FITKAcousticsVTKGraphObjectFEAcoustic.h"
#include "FITKAcousticsVTKSharedDataManager.h"
#include "FITKAcousticsVTKGraphActor.h"

#include "FITK_Interface/FITKVTKAlgorithm/FITKGraphActor.h"
#include "FITK_Component/FITKAcousticsModel/FITKAcousticsFEModel.h"
#include "FITK_Component/FITKAcousticsModel/FITKAbstractFESubSys.h"

#include <vtkProperty.h>
#include <vtkMapper.h>
#include <vtkLookupTable.h>
#include <vtkCompositeDataGeometryFilter.h>
#include <vtkMultiBlockDataSet.h>
#include <vtkExtractBlock.h>

namespace Exchange
{
    FITKAcousticsVTKGraphObjectFEAcoustic::FITKAcousticsVTKGraphObjectFEAcoustic(Acoustics::FITKAcousticsFEModel * model):
        FITKAcousticsVTKGraphObjectFESubSystems(model)
    {
        if (!model) {
            return;
        }

        //数据初始化
        init();

        //数据连接
        if (!generateDataSet()) {
            return;
        }

        //初始化演员
        generateGraph();
        //更新可见性
        updateVisibility();
        //渲染类型初始化
        m_graphInfo.Type = FITKAcousticsVTKCommons::FEAcousticGraph;
    }

    FITKAcousticsVTKGraphObjectFEAcoustic::~FITKAcousticsVTKGraphObjectFEAcoustic()
    {

    }

    bool FITKAcousticsVTKGraphObjectFEAcoustic::getDataVisibility()
    {
        // Get the runtime settings for visibility. 
        bool showSubSys = getRuntimeSettingAs<bool>(RTS_KEY_VISIBLE_SUB_SYSTEMS, true);
        bool showFE = getRuntimeSettingAs<bool>(RTS_KEY_VISIBLE_FE, true);
        bool showFEAcoustic = getRuntimeSettingAs<bool>(RTS_KEY_VISIBLE_FE_ACOUSTICS, true);
        bool isShow = showFE && showSubSys && showFEAcoustic;
        return isShow;
    }

    void FITKAcousticsVTKGraphObjectFEAcoustic::updateVisibility()
    {
        Acoustics::FITKAcousticsFEModel* feMode = dynamic_cast<Acoustics::FITKAcousticsFEModel*>(_dataObj);
        if (feMode == nullptr)return;
        Acoustics::FITKAcousticsFESubSysManager* feSubManager = feMode->getFESubSysManager();
        if (feSubManager == nullptr)return;

        int index = 0;
        if (m_extractBlock && m_multiBlockDataSet)
        {
            //清空
            m_multiBlockDataSet->ReleaseData();
            m_extractBlock->RemoveAllIndices();

            for (Acoustics::FITKAbstractFESubSys* subSys : feSubManager->getSubSysByType(Acoustics::FITKAbstractFESubSys::FESubSysType::FEAcoustic)) {
                if (!subSys->isEnable()) {
                    continue;
                }

                int dataIndex = -1;
                vtkDataSet* dataSet = FITKAcousticsVTKSharedDataManager::GetInstance()->getFESubSystemDataSet(subSys->getDataObjectID(), dataIndex);
                if (dataIndex < 0 || dataSet == nullptr) {
                    continue;
                }

                updateById(subSys->getDataObjectID(), false);

                m_multiBlockDataSet->SetBlock(index, dataSet);
                m_extractBlock->AddIndex(index);
                index++;
            }
        }

        // Update the whole object's visibility.
        this->Superclass::updateVisibility();
    }

    void FITKAcousticsVTKGraphObjectFEAcoustic::init()
    {
        m_FESubSysTypeEnum = Acoustics::FITKAbstractFESubSys::FEAcoustic;
        m_polygonOffset = FITKAcousticsVTKCommons::s_polygonOffsetFECavity;
        this->Superclass::init();
    }

    bool FITKAcousticsVTKGraphObjectFEAcoustic::generateDataSet()
    {
        return true;
    }

    void FITKAcousticsVTKGraphObjectFEAcoustic::generateGraph()
    {
        this->Superclass::generateGraph();
        m_fActor->GetProperty()->SetEdgeVisibility(true);

        //初始化颜色表
        initLookUptable(m_fActor->GetMapper()->GetLookupTable());
        //更新数据对象颜色
        updateByDataColor();
    }
}